package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.result.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @Description: TODO
 * @Author: 伍家钰
 * @Date: 2020/7/11 0:41
 * @Version: 1.0
 */
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Role record, Long[] ids) {
        roleMapper.insert(record);
        if (ids != null && ids.length != 0) {
            for (Long permissionId : ids) {
                roleMapper.insertRelation(record.getId(), permissionId);
            }
        }
    }

    @Override
    public Role selectById(Long id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        return role;
    }

    @Override
    public List<Role> selectAll() {
        List<Role> roles = roleMapper.selectAll();
        return roles;
    }

    @Override
    public void update(Role record, Long[] ids) {
        roleMapper.updateByPrimaryKey(record);
        // 清空中间表中对应的数据
        roleMapper.deleteRelation(record.getId());
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                roleMapper.insertRelation(record.getId(), id);
            }
        }
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        //查询数据总数
        List<Role> roles = roleMapper.selectForList(qo);
        return new PageInfo<Role>(roles);
    }

    @Override
    public List<Role> selectRoleById(Long id) {
        List<Role> roles = roleMapper.selectRoleById(id);
        return roles;
    }

    /**
     * 根据登入员工id查询员工角色编码
     *
     * @param id
     * @return
     */
    @Override
    public List<String> selectSnsByEmployeeId(Long id) {
        List<String> snss = roleMapper.selectSnsByEmployeeId(id);
        return snss;
    }
}
